package com.eyeem.recyclerviewtools.sample.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.eyeem.recyclerviewtools.LoadMoreOnScrollListener;
import com.eyeem.recyclerviewtools.StaggeredLayoutManagerUtils;
import com.eyeem.recyclerviewtools.adapter.WrapAdapter;
import com.eyeem.recyclerviewtools.sample.R;
import com.eyeem.recyclerviewtools.sample.adapter.DataAdapter;
import com.eyeem.recyclerviewtools.sample.data.Data;


public class ViewPagerFragment extends Fragment implements LoadMoreOnScrollListener.Listener {

    private RecyclerView recycler;
    private DataAdapter adapter;
    private WrapAdapter wrapAdapter;
    private View customView;

    public ViewPagerFragment() {
        // Required empty public constructor
    }

    public static ViewPagerFragment newInstance() {
        ViewPagerFragment fragment = new ViewPagerFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_viewpager, container, false);
        recycler = (RecyclerView) view.findViewById(R.id.recycler);
        init();
        return view;
    }

//    public void onSnapDirection(AppBarStateChangeListener.Direction direction) {
//        recycler.getParent().requestDisallowInterceptTouchEvent(false);
////        if (recycler.isNestedScrollingEnabled()) {
////            recycler.setNestedScrollingEnabled(false);
////        }
//    }
//
//    public void onStateChanged(AppBarLayout appBarLayout, AppBarStateChangeListener.State state) {
//        if (state == AppBarStateChangeListener.State.COLLAPSED) {
//            recycler.getParent().requestDisallowInterceptTouchEvent(true);
////            if (!recycler.isNestedScrollingEnabled()) {
////                recycler.setNestedScrollingEnabled(true);
////            }
//        }
//    }

    private void init() {
        // Layout manager
        RecyclerView.LayoutManager layoutManager;
        layoutManager = new LinearLayoutManager(getActivity());
        recycler.setLayoutManager(layoutManager);
        // Adapter
        adapter = DataAdapter.generateRandom(getActivity());
        wrapAdapter = new WrapAdapter(adapter);
        // Header
        //wrapAdapter.addHeader(new Header(getActivity()));
        // Load more scroller
        recycler.addOnScrollListener(new LoadMoreOnScrollListener(this));
        recycler.setAdapter(wrapAdapter);
        //recycler.setNestedScrollingEnabled(false);
    }

    public void onButtonPressed(Uri uri) {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onLoadMore(RecyclerView recyclerView) {
        //toast("Load more");
        wrapAdapter.showLoadMore();
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    adapter.data.add(Data.newRandom(getActivity()));
                }
                if (recycler.getLayoutManager() instanceof StaggeredGridLayoutManager) {
                    StaggeredLayoutManagerUtils.onItemChanged(recycler, 0);
                }
                adapter.notifyDataSetChanged();
                wrapAdapter.hideLoadMore();
            }
        }, 1000);
    }

    private void setCustomView(boolean visible) {
        if (visible && customView == null) {
            customView = LayoutInflater.from(getActivity())
                    .inflate(R.layout.custom_view, recycler, false);
        }
        wrapAdapter.setCustomView(visible ? customView : null);
        if (visible) {
            //toast("Custom view on WrapAdapter");
        }
    }
}
